package at.fhj.mobsecdev.cookit.webrequests;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

/**
 * Gets the current timestamp from the web server
 */
public class GetTimestampWebCall {
	
	public static String getTimestamp()
	{
		StringBuffer timestamp = new StringBuffer();
		
		String webServiceUrl = WebServiceUrls.GET_TIMESTAMP;
		
		try {			
			HttpClient client = new DefaultHttpClient();
			HttpGet request = new HttpGet(webServiceUrl);
			HttpResponse response = client.execute(request);
	
			String line = "";
			
			BufferedReader rd = new BufferedReader(new InputStreamReader(
					response.getEntity().getContent()));
			while((line = rd.readLine()) != null) {
				timestamp.append(line);
			}
		}
		catch(ClientProtocolException e) {
			throw new IllegalStateException("Could not connect to the web service");
		}
		catch(IOException e) {
			throw new IllegalStateException("Could not connect to the web service");
		}
		
		return timestamp.toString();
	}
}
